﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class BonusPromotions : System.Web.UI.Page
    {
        private int OrderInRank = 1;
        private int CurLeague = 0;
        private int CurGroup = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.BonusPromotions_PageTitle;
            //if (!HttpContext.Current.User.Identity.IsAuthenticated)
            //    Response.Redirect("Default.aspx");
            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();

            //if (ou.TeamId == "Null")
            //    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

           

            string LeagueId = "";
            if (!HttpContext.Current.User.Identity.IsAuthenticated && (Request.QueryString["LeagueId"] == null))
                Response.Redirect("~/Default.aspx");
            if (Request.QueryString["LeagueId"] != null)
                LeagueId = Request.QueryString["LeagueId"];
            else
            {
                LeagueId = GND.ReadNivdalIntData("UserId", "Nivdal_GetUserLeague", ou.UserId);
            }
             
            string CountryId = GND.ReadNivdalIntData("LeagueId", "Nivdal_GetCountryByLeague", LeagueId);
            CountryHyperlink.Text = GND.ReadNivdalStringData("CountryId", "Nivdal_GetCountryNameByIdProc", CountryId);
            CountryHyperlink.NavigateUrl = "Country.aspx?Id=" + CountryId;

            if (!IsPostBack)
            {
                BonusPromotionCountryInfoDataSource.SelectParameters.Add("LeagueId", LeagueId);
                LevelsDataSource.SelectParameters.Add("CountryId", CountryId);
                bool PromotionsSignupOpen = GND.ReadNivdalBoolData("Nivdal_IsBonusPromotionsSignupOpen");
                bool PromotionsInfoOpen = GND.ReadNivdalBoolData("Nivdal_BonusPromotionsStatusVisible");
                if (PromotionsInfoOpen)
                {
                    BonusPromotionsInvisible.Visible = false;
                    if (PromotionsSignupOpen)
                    {

                        string signupStatus = GND.ReadNivdalIntData("TeamId", "Nivdal_BonusPromotionCanUserSignUp", ou.TeamId);
                        if (signupStatus != "1")
                        {
                            SignupButton.Visible = false;
                            SignupStatusLabel.Text = Resources.Resource.BonusPromotions_SignedForBonusPromotion;
                        }
                        else
                        {
                            SignupButton.Visible = true;
                            SignupStatusLabel.Text = Resources.Resource.BonusPromotions_SignupForPromotionLabelText;
                        }
                    }
                    else
                    {
                        LevelsRepeater.Visible = false;
                        PromotionsOpenPanel.Visible = false;
                    }
                }
                else
                {
                    BonusPromotionsVisible.Visible = false;
                    BonusPromotionsInvisible.Visible = true;
                }
            }

            
                    
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void SignupClick(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            GND.ExecNivdalSQL("@TeamId", "Nivdal_BonusPromotionSignUp", ou.TeamId);
            SignupStatusLabel.Text = Resources.Resource.BonusPromotions_SignedForBonusPromotion;
            SignupButton.Visible = false;
        }

        protected string GetOrderInRank(object _League, object _Group)
        {
            int Group, League;
            Int32.TryParse(_Group.ToString(), out Group);
            Int32.TryParse(_League.ToString(), out League);

            if (League == CurLeague)
            {
                if (Group == CurGroup)
                {
                    OrderInRank++;
                }
                else
                {
                    CurGroup = Group;
                    OrderInRank = 1;
                }
            }
            else
            {
                CurLeague = League;
                CurGroup = Group;
                OrderInRank = 1;
            }

            return (OrderInRank.ToString());
        }
    }
}